import 'dart:io';

import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flutter_app/router/route.dart';
import 'package:flutter_app/util/LogUtil.dart';
import 'package:flutter_app/util/shared_preferences.dart';
import 'package:flutter_app/view/grideview_page.dart';

import 'home.dart';

// 声明库，名ballgame
void main() {
  runApp(MyApp());
  if (Platform.isAndroid) {
    // var info = Platform.packageConfig +
    //     ' ---- ' +
    //     Platform.localHostname +
    //     ' ---- ' +
    //     (Platform.isAndroid ? "Android手机" : "IOS手机");
    LogUtil.e("手机是Android ", tag:  (Platform.isAndroid ? "Android手机" : "IOS手机"));
    //初始化状态
    LogUtil.init();
    //设置Android头部的导航栏透明
    SystemUiOverlayStyle systemUiOverlayStyle = SystemUiOverlayStyle(
        statusBarColor: Colors.transparent, //全局设置透明
        statusBarIconBrightness: Brightness.light
        //light:黑色图标 dark：白色图标
        //在此处设置statusBarIconBrightness为全局设置
        );
    SystemChrome.setSystemUIOverlayStyle(systemUiOverlayStyle);
  }
  //sp初始化完毕
  SPUtils.init().then((value) => print("SPUtils初始化完成......"));

}

///MyHomePage是一个控件
class MyApp extends StatelessWidget {
  // This widget is the root of your application.
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      //加入下面代码即可
      debugShowCheckedModeBanner: false,
      theme: ThemeData(
        primarySwatch: Colors.blue,
        visualDensity: VisualDensity.adaptivePlatformDensity,
      ),
      home: HomeApp(),
    );
  }
}

/// 主页
class HomeApp extends StatefulWidget {
  @override
  State<StatefulWidget> createState() {
    return new _HomeAppState();
  }
}

class _HomeAppState extends State<HomeApp> with SingleTickerProviderStateMixin {
  TabController? _tabController; //需要定义一个Controller
  List tabs = ["基础", "功能", "扩展"];
  String ?title; //标题
  var _selectedIndex = 0;
  List<Widget> getTabViews(BuildContext context) => <Widget>[
        MyHomePage(),
        GridViewPage(items: RouteMap.getFunItems(context)),
        GridViewPage(items: RouteMap.getExpandItems(context)),
      ];

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text("玩Flutter"),
        bottom: TabBar(
          //生成Tab菜单
          controller: _tabController, //标签选择变化控制器
          tabs: tabs.map((e) => Tab(text: e)).toList(),
          onTap: _onItemTapped,
          indicatorWeight :1.5,//选中下划线的高度，值越大高度越高，默认为2
          indicatorPadding: EdgeInsets.all(1), //字体padding
          indicatorSize: TabBarIndicatorSize.label, //指示器长短，tab：和tab一样长，label：和标签label 一样长
          // labelPadding: EdgeInsetsGeometry.infinity,
          labelColor: Colors.white, //标签颜色

          labelStyle: TextStyle(fontSize: 18),
          unselectedLabelColor: Color(0xFFD6D6D6), ////设置未选中时的字体颜色，tabs里面的字体样式优先级最高
          unselectedLabelStyle: TextStyle(fontSize: 15), ////设置未选中时的字体样式，tabs里面的字体样式优先级最高
          // indicator: const BoxDecoration(), //,//用于设定选中状态下的展示样式不要下划线


        ),
      ),
      body: TabBarView(
        //联动
        controller: _tabController,
        children: getTabViews(context).map((Widget widget) {
          return widget;
        }).toList(),
      ),
    );
  }

  void _onItemTapped(int index) {
    setState(() {
      _selectedIndex = index;
    });
  }

  @override
  void initState() {
    super.initState();
    _tabController = TabController(length: tabs.length, vsync: this);
  }
}
